package org.edu.eece262.taskshare;

import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class InviteContacts extends Activity {

	LinearLayout llContacts;
	GroupDBAdapter groupDBA;
	ContactsDBAdapter contactsDBA;
	Button bInviteMembersGroup;
	Button bCancelInviteGroup;
	TextView tvGroupName;
	public static final String PREFS_NAME = "TASKSHARELOGIN";

	LinkedList<Messages.GroupMember> newMembers;

	Intent results;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.creategroup);

		groupDBA = GroupDBAdapter.getGroupDBA(getApplicationContext());
		contactsDBA = ContactsDBAdapter
				.getContactsDBAdapter(getApplicationContext());

		bInviteMembersGroup = (Button) findViewById(R.id.bCreateNewGroup);
		bCancelInviteGroup = (Button) findViewById(R.id.bCancelGroupCreate);
		llContacts = (LinearLayout) findViewById(R.id.llContactsNewGroup);
		tvGroupName = (TextView) findViewById(R.id.tvNewGroupName);
		
		bInviteMembersGroup.setText("Invite");

		newMembers = new LinkedList<Messages.GroupMember>();

		results = new Intent();

		tvGroupName.setText(getIntent().getStringExtra("group_name"));

		contactsDBA.open();
		Cursor contacts = contactsDBA.getContacts();
		contacts.moveToFirst();
		
		LinkedList<String> currentMembers = new LinkedList<String>();
		currentMembers = groupDBA.getGroupMemberNames(getIntent().getStringExtra("group_name"));
		LinkedList<Integer> currentMembersStatus = new LinkedList<Integer>();
		currentMembersStatus = groupDBA.getGroupMemberStatus(getIntent().getStringExtra("group_name"));
		LinkedList<Integer> currentMembersRoll = new LinkedList<Integer>();
		currentMembersRoll = groupDBA.getGroupMemberRoll(getIntent().getStringExtra("group_name"));
		
		for (int i=0; i < currentMembers.size(); ++i) {
			newMembers.add(new Messages.GroupMember(currentMembers.get(i), currentMembersStatus.get(i), currentMembersRoll.get(i)));
		}
		
		int j = 0;
		for (; !contacts.isAfterLast(); contacts.moveToNext()) {
			if (!currentMembers.contains(contacts.getString(ContactsDBAdapter.USER_NAME_COLUMN))) {
				CheckBox current = new CheckBox(this);
				current.setText(contacts
						.getString(ContactsDBAdapter.USER_NAME_COLUMN));
				current.setChecked(false);
	
				current.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						Messages.GroupMember tmp = new Messages.GroupMember(
								buttonView.getText().toString(),
								Messages.GroupMember.StatusType.REQUESTED,
								Messages.GroupMember.RollType.USER);
						if (isChecked)
							newMembers.add(tmp);
						else
							newMembers.remove(tmp);
					}
				});
	
				llContacts.addView(current, j, new ViewGroup.LayoutParams(
						ViewGroup.LayoutParams.FILL_PARENT,
						ViewGroup.LayoutParams.WRAP_CONTENT));
				++j;
			}
		}
		contactsDBA.close();

		bInviteMembersGroup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				GroupDBAdapter groupDBA = GroupDBAdapter.getGroupDBA(getApplicationContext());
				SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
				groupDBA.addMembers(tvGroupName.getText().toString(), settings.getString("user", ""), newMembers, getApplicationContext());
				finish();
			}
		});

		bCancelInviteGroup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

}
